package com.yequan.hbb.view.fragment;


import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.yequan.hbb.R;
import com.yequan.hbb.adapter.HPCAdapter;
import com.yequan.hbb.entries.FocusBean;
import com.yequan.hbb.entries.RecentBean;
import com.yequan.hbb.servers.Servers;
import com.yequan.hbb.utils.ServerUtil;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class HBCircleFragment extends Fragment {


    private RecyclerView recycler;
    private SwipeRefreshLayout swipeRefreshLayout;
    private HPCAdapter adapter;

    public HBCircleFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_hbcircle, container, false);
        recycler = ((RecyclerView) view.findViewById(R.id.recycler));
        recycler.setLayoutManager(new LinearLayoutManager(getActivity()));

        swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.srl);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        swipeRefreshLayout.setRefreshing(false);
                    }
                },3000);
            }
        });
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        FragmentManager manager = getChildFragmentManager();
        adapter = new HPCAdapter(getContext(), manager);
        ServerUtil.createHttpRequest(Servers.class).getFocusBean(true, "android-208984F9-8FBF-0000-8643-941020898468&1476189600143").enqueue(new Callback<FocusBean>() {
            @Override
            public void onResponse(Call<FocusBean> call, Response<FocusBean> response) {
                adapter.add(response.body());
                adapter.add(new RecentBean());
                recycler.setAdapter(adapter);
            }

            @Override
            public void onFailure(Call<FocusBean> call, Throwable t) {

            }
        });

    }
}
